package com.ly.canal.utils;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.ass.canal.annotation.CanalEventListener;
import com.ass.canal.annotation.ListenPoint;

import java.util.List;

/**
 * @author jigua
 * @version 1.0
 * @className CanalDataEventListener
 * @description
 * @create 2022/3/1 18:40
 */
@CanalEventListener
public class CanalDataEventListener {


    int i = 0;
    /**
     * 数据增量同步
     * 实现业务逻辑
     */
//    @UpdateListenPoint
    public void test(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        //数据变更前
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList) {
            System.out.println(column.getName() + ":" + column.getValue());
        }
        //数据变更后
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            System.out.println(column.getName() + ":" + column.getValue());
        }
    }

//    @InsertListenPoint
    public void test1(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        //数据变更前
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList) {
            System.out.println(column.getName() + ":" + column.getValue());
        }
    }

//    @DeleteListenPoint
    public void test2(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        //数据变更前
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList) {
            System.out.println(column.getName() + ":" + column.getValue());
        }
    }

    /**
     * destination 指定监听实例
     * schema	指定监听数据库名称
     * table	指定表名 {"user"} || {"user","user_info"}
     * eventType 指定监听CURD类型
     */
    @ListenPoint(destination = "example", schema = "ry-cloud", table = {"iot_area","sys_user"}
            , eventType = {CanalEntry.EventType.INSERT,CanalEntry.EventType.UPDATE})
    public void test3(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        System.out.println("i: "+i);
        System.out.println("执行了: "+eventType);
        //数据变更前
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        for (CanalEntry.Column column : beforeColumnsList) {
            System.out.println("数据变更前: "+column.getName() + ":" + column.getValue());
        }
        //数据变更后
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            System.out.println("数据变更后: "+column.getName() + ":" + column.getValue());
        }
        i++;
    }


    private void removeCatch(){
        //如果是新增操作


        //如果是update操作,需要关注下某些字段是否变化

        //如果是delete操作



    }


    private boolean isDataChangeByColumn(){


        return false;
    }




}

